package uy.edu.fing.pgsegesb.test;

import javax.crypto.SecretKey;


import org.jboss.soa.esb.message.Body;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.actions.annotation.Process;
import org.jboss.soa.esb.configure.ConfigProperty;
import org.w3c.dom.Document;
import org.apache.xml.security.encryption.XMLCipher;  

import uy.edu.fing.pgsegesb.encryption.CipherHelper;
import uy.edu.fing.pgsegesb.utils.XmlHelper;

public class EncryptTripleDes2 {
	
	@ConfigProperty
	private String kek;
	
	@ConfigProperty
	private String encDataElement;

	@Process
	public void encrypt(Message message) {
		System.out.println("Encriptando con " + kek + " : " + encDataElement + " : "
				+ message.getBody().toString()); //.get("org.jboss.soa.esb.message.defaultEntry").toString()
		
		try {
			String stdes = kek;
			SecretKey ktdes = (SecretKey) CipherHelper.keyFromString(stdes, "TripleDES");
			// "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
			Document msgBody = XmlHelper.stringToXML( message.getBody().get("org.jboss.soa.esb.message.defaultEntry").toString());
			Document encMsgBody = CipherHelper.encrypt(ktdes, msgBody, encDataElement);
			String encMsgBodyStr = XmlHelper.xmlToString(encMsgBody);
			System.out.println();
			System.out.println("------------------------------------");
			System.out.println("BOdy cifrado: " + encMsgBodyStr);
			System.out.println("------------------------------------");
			
			message.getBody().add("org.jboss.soa.esb.message.defaultEntry", encMsgBodyStr);
			//body.add(XmlHelper.xmlToString(encMsgBody));
			
			System.out.println("Mensaje cifrado: " + message.getBody().toString());
			
		} catch (Exception e) {e.printStackTrace();}
	}

	@Process
	public void decrypt(Message message) {
		System.out.print("Descifrando " + message.getBody().toString());
		try{
		
			
			Document encMsgBody = XmlHelper.stringToXML(message.getBody().toString());
			Document msgBody = CipherHelper.decrypt(encMsgBody, (SecretKey) CipherHelper.keyFromString(kek, XMLCipher.TRIPLEDES_KeyWrap));
			System.out.println();
			System.out.println("------------------------------------");
			System.out.println("BOdy cifrado: " + message.getBody().toString());
			System.out.println("------------------------------------");
			message.getBody().replace((Body) msgBody);
			
			System.out.println("Mensaje cifrado: " + message.getBody().toString());
			
		} catch (Exception e) {e.printStackTrace();}
		
	}
	


/*
	public static String encrypt(SecretKey key, String text) {
		String result = "";
		try {
			InputStream in = new ByteArrayInputStream(text.getBytes("UTF-8"));
			OutputStream out;
			// Create and initialize the encryption engine
			Cipher cipher = Cipher.getInstance("DESede");
			cipher.init(Cipher.ENCRYPT_MODE, key);

			// Create a special output stream to do the work for us
			CipherOutputStream cos = new CipherOutputStream(out, cipher);

			// Read from the input and write to the encrypting output stream
			byte[] buffer = new byte[2048];
			int bytesRead;
			while ((bytesRead = in.read(buffer)) != -1) {
				cos.write(buffer, 0, bytesRead);
			}
			cos.close();

			// For extra security, don't leave any plaintext hanging around
			// memory.
			java.util.Arrays.fill(buffer, (byte) 0);
			result = out.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void decrypt(SecretKey key, InputStream in, OutputStream out) {
		try {
			// Create and initialize the decryption engine
			Cipher cipher = Cipher.getInstance("DESede");
			cipher.init(Cipher.DECRYPT_MODE, key);

			// Read bytes, decrypt, and write them out.
			byte[] buffer = new byte[2048];
			int bytesRead;
			while ((bytesRead = in.read(buffer)) != -1) {
				out.write(cipher.update(buffer, 0, bytesRead));
			}
			// Write out the final bunch of decrypted bytes
			out.write(cipher.doFinal());
			out.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	*/
	
	

	
	
}
